<template>
  <div class="allTop">
    <el-dialog
      title="修改农资"
      :visible.sync="visible"
      @close="$emit('update:show', false)"
      :show="show"
      width="1000px"
    >
      <div
        style="font-weight: 700;
    font-size: 20px;
    margin-left: 20px;
    padding-bottom: 10px;"
      >
        修改农资信息
      </div>
      <div class="main">
        <div class="main_0">
          <el-form
            ref="form"
            :model="formlate"
            :inline="true"
            class="demo-form-inline"
          >
            <div>
              <el-form-item label="名称：">
                <el-input
                  v-model="formlate.goodsName"
                  placeholder="请输入名称"
                ></el-input>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="类型：">
                <el-cascader
                  v-model="cascader"
                  :options="seleList"
                  :props="{
                    checkStrictly: true,
                    value: 'id',
                    label: 'goodName',
                    children: 'children',
                    expandTrigger: 'hover'
                  }"
                  clearable
                  ref="cascaderAddr"
                  @change="newSelet"
                ></el-cascader>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="型号：">
                <el-input
                  v-model="formlate.model"
                  placeholder="请填写型号名称"
                ></el-input>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="规格：">
                <el-input
                  v-model="formlate.specs"
                  placeholder="请填写数量"
                  style="width:120px"
                ></el-input>
              </el-form-item>
              <el-form-item label="计量单位：">
                <el-select
                  v-model="formlate.meteringUnit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_1"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="计数单位：">
                <el-select
                  v-model="formlate.unit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_2"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="保质期：">
                <el-input
                  v-model="formlate.period"
                  placeholder="请填写保质期"
                ></el-input>
              </el-form-item>
              <el-form-item label="单位：">
                <el-select
                  v-model="formlate.periodUnit"
                  placeholder="请选择"
                  style="width:120px"
                >
                  <el-option
                    v-for="item in options_3"
                    :key="item.dictSort"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item label="生产商：">
                <el-input
                  v-model="formlate.supplier"
                  placeholder="请填写生产商名"
                ></el-input>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="main_1">
          <div
            style="position:absolute;top:200px;right:100px;z-index:999999;width:120px;height:160px;"
          >
            <el-upload
              class="driver-uploader"
              action="iotAction"
              :show-file-list="false"
              :http-request="uploadSectionFile"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div style="text-align:center;line-height:40px;">添加照片</div>
          </div>
        </div>
      </div>
      <div slot="footer">
        <el-button
          @click="complete"
          style="    color: #fff;
    background-color: #11C26D;
    border-color: #11C26D;"
          >完 成</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { upload } from "@/api/upload";
import { querynzAdd, querynzmodify,querynzmlList ,querynzmlDetail} from "@/api/manage";
import { dictionariesCheck } from "@/api/dictionaries";
import { typelistMaterial } from "@/api/material";
export default {
  data() {
    return {
      visible: false,
      cascader: [],
      formlate: {
        id: null,
        goodsName: null,
        model: null,
        specs: null,
        period: null,
        periodUnit: null,
        supplier: null,
        meteringUnit: "",
        unit: null,
        brand: null,
        limitDown: null
        // goodsfile:null
      },
      imageUrl: "",
      options_0: [],
      options: [
        {
          value: 0,
          label: "种子"
        },
        {
          value: 1,
          label: "化肥"
        },
        {
          value: 2,
          label: "农药"
        }
      ],
      options_1: [],
      options_2: [],
      options_3: [],
      seleList: []
    };
  },
  methods: {
    newSelet(val) {
            setTimeout(() => {
        let a = this.$refs["cascaderAddr"].inputValue
      }, 1);
            let c = val[val.length-1]
      this.formlate.goodsType = c
    },
    complete() {
      this.visible = false;
      querynzmodify(this.formlate).then(res => {
        console.log(res)
        querynzmlList({ isWarn: 0 }).then(res => {
          console.log(res)
          // this.formlate = res.data.rows
        });
      });
    },
    show(status, row) {
      console.log(row);
      this.visible = status;

      dictionariesCheck("metering_unit").then(res => {
        this.options_1 = res.data.data;
      });
      dictionariesCheck("goods_unit").then(res => {
        this.options_2 = res.data.data;
      });
      dictionariesCheck("period_unit").then(res => {
        this.options_3 = res.data.data;
      });
      typelistMaterial({ goodType: "farm_good" }).then(res => {
        const rootObj = [
          {
            id: 1,
            path: 0,
            parentId: 0,
            goodType: "farm_good",
            goodName: "农资",
            children: res.data.data
          }
        ];
        this.seleList = rootObj;
      });
      querynzmlDetail(row.id).then(respone=>{
        console.log(respone)
      })
      this.formlate = row;
      setTimeout(() => {
        this.$refs["cascaderAddr"].inputValue = row.goodsTypeName;
      }, 1);
    },
    //照片上传
    uploadSectionFile({ file, onError, onSuccess }) {
      console.log(file);
      const fileType = file.type,
        fileName = file.name,
        isImage = fileType.indexOf("image") != -1,
        isLt500KB = file.size / 1024 < 500;
      // 这里常规检验，看项目需求而定
      if (!isImage) {
        this.$message.error("只能上传图片格式png、jpg、gif!");
        return;
      }
      if (!isLt500KB) {
        this.$message.error("只能上传图片大小小于500KB");
        return;
      }
      const formData = new FormData();
      formData.append("file", file);
      const loading = this.$load(this);
      upload(formData)
        .then(res => {
          if (res.data.code == 200) {
            this.form.photoUrl = res.data.data.path;
            this.imageUrl = res.data.data.url + this.form.photoUrl;
          }
          loading.close();
        })
        .catch(err => {
          console.log(err);
          loading.close();
          this.$message({ type: "error", message: "上传异常:" + err });
        });
    }
  }
};
</script>

<style scoped>
.allTop >>> .el-dialog__header {
  background: #009865;
}
.allTop >>> .el-dialog__title {
  color: white;
  font-size: 16px;
}
.allTop >>> .el-dialog__header {
  padding: 10px 10px 10px;
}
.allTop >>> .el-dialog__headerbtn {
  position: absolute;
  top: 15px;
  right: 20px;
  padding: 0;
  background: 0 0;
  border: none;
  outline: 0;
  cursor: pointer;
  font-size: 16px;
}
.main {
  width: 100%;
  height: 400px;
  /* background: red; */
  display: flex;
}
.main_0 {
  height: 100%;
  width: 70%;
  /* background: yellow; */
}
.main_1 {
  flex: 1;
  /* background: violet; */
}
.demo-form-inline >>> .el-form-item__label {
  width: 82px;
}
.driver-uploader {
  border: 1px solid #606266;
}

.driver-uploader .el-upload {
  border: 1px dashed #009865;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.driver-uploader .el-upload:hover {
  border-color: #009865;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #009865;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
